
foreach yr in 11 12 13 14 {

use "$temp\IP claims processed with AHA HNPI - 20`yr'.dta", clear

	if `yr' == 14 {
		replace ntwrk_ind = 0 if ntwrk_ind == .
		replace primary_cov_ind = "" if primary_cov_ind == "U"
		destring primary_cov_ind, replace
		replace primary_cov_ind = -1 if primary_cov_ind == . 
	}
	
	else {
		replace ntwrk_ind = 0
		replace primary_cov_ind = 0
	}
	
save "$temp\raw claims.dta", replace
	
*collapse IP claims to admissions
collapse (sum) charge amt_net_paid coins copay deduct calc_allwd (max) er_flag newborn_flag ntwrk_ind primary_cov_ind, by(z_patid dstatus fst_admtdt last_dischdt ///
mdc drg age_band_cd gdr mbr_zip_5_cd prod mkt_sgmnt_cd funding aha_hnpi) fast
	
*find admits with multiple records
duplicates tag z_patid fst_admtdt, gen(dup)
tab dup
	
*Process non-duplicates and save
preserve
	keep if dup == 0
	drop dup 
		
	save "$temp\claims_clean_pt1.dta", replace 
restore
	
*Clean duplicates 
drop if dup == 0
drop dup
	
contract z_patid fst_admtdt  
merge 1:m z_patid fst_admtdt using "$temp\raw claims.dta"
keep if _m == 3
drop _m _f 
	
bysort admit_id: egen max_stay = max(last_dischdt)
replace last_dischdt = max_stay
drop max_stay
	
collapse (sum) charge amt_net_paid coins copay deduct calc_allwd (max) er_flag newborn_flag ntwrk_ind primary_cov_ind, by(z_patid dstatus fst_admtdt last_dischdt ///
mdc drg age_band_cd gdr mbr_zip_5_cd prod mkt_sgmnt_cd funding aha_hnpi) fast

duplicates tag z_patid fst_admtdt, gen(dup)
tab dup
drop dup
	
replace dstatus = 0 if dstatus == .
gsort z_patid fst_admtdt last_dischdt -dstatus
replace dstatus = dstatus[_n-1] if dstatus == 0 & z_patid == z_patid[_n-1] & fst_admtdt == fst_admtdt[_n-1] & last_dischdt == last_dischdt[_n-1]
	
gsort z_patid fst_admtdt last_dischdt
replace aha_hnpi = aha_hnpi[_n-1] if aha_hnpi == "" & z_patid == z_patid[_n-1] & fst_admtdt == fst_admtdt[_n-1] & last_dischdt == last_dischdt[_n-1]
	
collapse (sum) charge amt_net_paid coins copay deduct calc_allwd (max) er_flag newborn_flag ntwrk_ind primary_cov_ind, by(z_patid dstatus fst_admtdt last_dischdt ///
mdc drg age_band_cd gdr mbr_zip_5_cd prod mkt_sgmnt_cd funding aha_hnpi) fast

duplicates tag z_patid fst_admtdt, gen(dup)
tab dup

preserve
	keep if dup == 0 
	drop dup
			
	save "$temp\claims_clean_pt2.dta", replace
restore

use  "$temp\claims_clean_pt1.dta", clear
append using  "$temp\claims_clean_pt2.dta"
	
duplicates tag z_patid last_dischdt , gen(dup)
drop if dup>0
drop dup

drop if mbr_zip_5_cd == 99999 | mbr_zip_5_cd == .	
drop if drg == .
drop if mdc == ""
drop if drg == 998 | drg == 999
drop if mdc == "PR" | mdc == "AL"
tab mdc
	
gen los = last_dischdt - fst_admtdt 
assert los != .
sum los, det
drop if los < 1
drop if los > r(p99)
	
drop if calc_allwd <= 0
drop if charge <= 0
	
save "$temp\claims_clean`yr'.dta", replace

}
	
exit
